cmake_minimum_required(VERSION 3.8)
project(semantic_map_manager)

# Default to C++17
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 17)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# Find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_components REQUIRED)
find_package(std_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(visualization_msgs REQUIRED)
find_package(tf2 REQUIRED)
find_package(tf2_ros REQUIRED)
find_package(tf2_geometry_msgs REQUIRED)
find_package(nav_msgs REQUIRED)

find_package(OpenCV REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(Boost REQUIRED COMPONENTS system)
find_package(common REQUIRED)
find_package(motion_predictor REQUIRED)
find_package(vehicle_msgs REQUIRED)
find_package(vehicle_model REQUIRED)

set(THIRD_PARTY_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/thirdparty)

# Include directories
include_directories(
    include
    ${THIRD_PARTY_INCLUDE_DIRS}
    ${vehicle_msgs_INCLUDE_DIRS}
    ${vehicle_model_INCLUDE_DIRS}
    ${common_INCLUDE_DIRS}
    ${motion_predictor_INCLUDE_DIRS}
    ${EIGEN3_INCLUDE_DIR}
    ${OpenCV_INCLUDE_DIRS}
)

# Create core library
add_library(hkust_pl_smm SHARED
    src/semantic_map_manager.cc
    src/data_renderer.cc
    src/config_loader.cc
    src/traffic_signal_manager.cc
)

# Add dependencies to core library
ament_target_dependencies(hkust_pl_smm
  rclcpp
  std_msgs
  geometry_msgs
  sensor_msgs
  visualization_msgs
)

target_link_libraries(hkust_pl_smm
    vehicle_model::vehicle_model
    common::hkust_pl_common
    motion_predictor::motion_predictor
    ${OpenCV_LIBS}
    ${Boost_LIBRARIES}
)

# Create ROS adapter library
add_library(hkust_smm_ros SHARED
    src/ros_adapter.cc
    src/visualizer.cc
)

# Add ROS2 dependencies to adapter library
ament_target_dependencies(hkust_smm_ros
  rclcpp
  rclcpp_components
  std_msgs
  geometry_msgs
  sensor_msgs
  visualization_msgs
  tf2
  tf2_ros
  tf2_geometry_msgs
  vehicle_msgs
  nav_msgs
)

target_link_libraries(hkust_smm_ros
    hkust_pl_smm
    ${Boost_LIBRARIES}
)

# Set target properties
target_include_directories(hkust_pl_smm PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>
)

target_include_directories(hkust_smm_ros PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>
)

# Install libraries
install(TARGETS hkust_pl_smm hkust_smm_ros
  EXPORT export_${PROJECT_NAME}
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib
  RUNTIME DESTINATION bin
)

# Install headers
install(DIRECTORY include/
  DESTINATION include
)

# Export dependencies
ament_export_targets(export_${PROJECT_NAME} HAS_LIBRARY_TARGET)
set(EXPORT_DEPENDENCIES
  rclcpp
  rclcpp_components
  std_msgs
  geometry_msgs
  sensor_msgs
  visualization_msgs
  tf2
  tf2_ros
  tf2_geometry_msgs
  vehicle_msgs
  nav_msgs
)
if(common_FOUND)
  list(APPEND EXPORT_DEPENDENCIES common)
endif()
if(vehicle_model_FOUND)
  list(APPEND EXPORT_DEPENDENCIES vehicle_model)
endif()
if(motion_predictor_FOUND)
  list(APPEND EXPORT_DEPENDENCIES motion_predictor)
endif()
ament_export_dependencies(${EXPORT_DEPENDENCIES})

# Export include directories
ament_export_include_directories(include)

# Testing
if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()